package org.jeecg.modules.rental.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.rental.entity.RentalEbikeShare;
import org.jeecg.modules.rental.resp.MyEBikeResp;
import org.jeecg.modules.rental.resp.ShareAcountResp;
import org.jeecg.modules.rental.resp.ShareEBikeResp;
import org.jeecg.modules.rental.service.IRentalEbikeShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 控制分享
 * @Author: sean
 * @Date:   2024-12-19
 * @Version: V1.0
 */
@Api(tags="控制分享")
@RestController
@RequestMapping("/rental/ebike-share")
@Slf4j
public class RentalEbikeShareController extends JeecgController<RentalEbikeShare, IRentalEbikeShareService> {
	@Autowired
	private IRentalEbikeShareService rentalEbikeShareService;
	
	/**
	 * 分页列表查询
	 *
	 * @param rentalEbikeShare
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "控制分享-分页列表查询")
	@ApiOperation(value="控制分享-分页列表查询", notes="控制分享-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<RentalEbikeShare>> queryPageList(RentalEbikeShare rentalEbikeShare,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<RentalEbikeShare> queryWrapper = QueryGenerator.initQueryWrapper(rentalEbikeShare, req.getParameterMap());
		Page<RentalEbikeShare> page = new Page<RentalEbikeShare>(pageNo, pageSize);
		IPage<RentalEbikeShare> pageList = rentalEbikeShareService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param rentalEbikeShare
	 * @return
	 */
	@AutoLog(value = "控制分享-添加")
	@ApiOperation(value="控制分享-添加", notes="控制分享-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:rental_ebike_share:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RentalEbikeShare rentalEbikeShare) {
		rentalEbikeShareService.save(rentalEbikeShare);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param rentalEbikeShare
	 * @return
	 */
	@AutoLog(value = "控制分享-编辑")
	@ApiOperation(value="控制分享-编辑", notes="控制分享-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:rental_ebike_share:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RentalEbikeShare rentalEbikeShare) {
		rentalEbikeShareService.updateById(rentalEbikeShare);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "控制分享-通过id删除")
	@ApiOperation(value="控制分享-通过id删除", notes="控制分享-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:rental_ebike_share:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		rentalEbikeShareService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "控制分享-批量删除")
	@ApiOperation(value="控制分享-批量删除", notes="控制分享-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:rental_ebike_share:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.rentalEbikeShareService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "控制分享-通过id查询")
	@ApiOperation(value="控制分享-通过id查询", notes="控制分享-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<RentalEbikeShare> queryById(@RequestParam(name="id",required=true) String id) {
		RentalEbikeShare rentalEbikeShare = rentalEbikeShareService.getById(id);
		if(rentalEbikeShare==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(rentalEbikeShare);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param rentalEbikeShare
    */
    //@RequiresPermissions("org.jeecg.modules.demo:rental_ebike_share:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RentalEbikeShare rentalEbikeShare) {
        return super.exportXls(request, rentalEbikeShare, RentalEbikeShare.class, "控制分享");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("rental_ebike_share:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RentalEbikeShare.class);
    }

	@ApiOperation("共享列表")
	@RequestMapping(value = "/share-list", method = RequestMethod.GET)
	public Result shareList(@RequestParam(value="ebikeId") Long ebikeId) {
		List<ShareAcountResp> shareList = rentalEbikeShareService.shareList(ebikeId);
		return Result.OK(shareList);
	}

	@ApiOperation("添加共享用户")
	@RequestMapping(value = "/add-share-account", method = RequestMethod.GET)
	public Result addShareAccount(
			@RequestParam(value="ebikeId") Long ebikeId,
			@RequestParam(value="accountCode") String accountCode) {
		rentalEbikeShareService.addShereAccount(ebikeId, accountCode);
		return Result.OK();
	}

	@ApiOperation("删除共享用户")
	@RequestMapping(value = "/remove-share-account", method = RequestMethod.GET)
	public Result removeShareAccount(@RequestParam(name="ids",required=true) String ids) {
		rentalEbikeShareService.removeShereAccount(ids);
		return Result.OK();
	}

	@ApiOperation("设置独占状态")
	@RequestMapping(value = "/set-ctrl-status", method = RequestMethod.GET)
	public Result setCtrlStatus(@RequestParam(name="id",required=true) Long id,
								@RequestParam(name="isCtrl",required=true) Boolean isCtrl) {
		rentalEbikeShareService.setCtrlStatus(id, isCtrl);
		return Result.OK();
	}

	@ApiOperation("占用车辆")
	@RequestMapping(value = "/ctrl-ebike", method = RequestMethod.GET)
	public Result ctrlEBike(@RequestParam(name="ebikeId",required=true) Long ebikeId) {
		rentalEbikeShareService.setCtrlEBike(ebikeId);
		return Result.OK();
	}

	@ApiOperation("结束占用车辆")
	@RequestMapping(value = "/end-ctrl", method = RequestMethod.GET)
	public Result endCtrl(@RequestParam(name="ebikeId",required=true) Long ebikeId) {
		rentalEbikeShareService.setEndCtrl(ebikeId);
		return Result.OK();
	}

	@ApiOperation("我的车辆列表")
	@RequestMapping(value = "/ebike-list", method = RequestMethod.GET)
	public Result ebikeList() {
		List<ShareEBikeResp> shareList = rentalEbikeShareService.ebikeList();
		return Result.OK(shareList);
	}

}
